package com.tibco.notepad.ui;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tibco.notepad.R;
import com.tibco.notepad.Service.PreferencesUtil;
import com.tibco.notepad.domain.Notepad;
import com.tibco.notepad.util.Constant;
import com.tibco.notepad.util.FileUtil;

public class WriteNewNoteActivity extends Activity {
	public PreferencesUtil mSharPreferenceUtil;
	public EditText title_edit;
	public EditText content_edit;
	private Button mSaveButton;
	private FileUtil mFileUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_note);
		mFileUtil = new FileUtil(this);
		initView();
		setListener();
	}

	private void initView() {
		title_edit = (EditText) findViewById(R.id.edit_title);
		content_edit = (EditText) findViewById(R.id.edit_content);
		mSaveButton = (Button) this.findViewById(R.id.notepad_save_button);
		mSharPreferenceUtil = new PreferencesUtil(this);
	}

	private void setListener() {
		mSaveButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				save();
				String title = title_edit.getText().toString();
				String content = content_edit.getText().toString();
				Notepad notepad = new Notepad(title, PreferencesUtil.getTime(),
						content);
				List<Notepad> list = mFileUtil.getNotepadList();
				list.add(notepad);
				mFileUtil.saveListToXml(list);
				setResult(10, null);
				finish();
			}
		});
	}

	/**
	 * 保存进SharedPreferences中
	 *
	 */
	private void save() {
		String title = title_edit.getText().toString();
		String content = content_edit.getText().toString();
		mSharPreferenceUtil.save(title, content);
		Toast.makeText(getApplicationContext(), R.string.success, 1).show();
	}

}
